/*
 *
 */

package org.darkforest.dao.dao.impl;

import org.apache.ibatis.session.SqlSessionFactory;
import org.darkforest.dao.dao.BaseDao;
import org.darkforest.dao.mapper.BaseMapper;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;


/**
 * 数据库公共类Dao类接口实现类
 */
public abstract  class BaseDaoImpl<T,E>  extends SqlSessionDaoSupport implements BaseDao<T,E> {

    protected Class<E> mapperClass;

    @Resource
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
        super.setSqlSessionFactory(sqlSessionFactory);
    }

    @Autowired
    protected SqlSessionTemplate sqlSessionTemplate;

    /**
     * 设置对应的MapperClass
     */
    public void setMapperClass(Class<E> mapperClass)
    {
        this.mapperClass = mapperClass;
    }

    /**
     * 得到对应的Mapper对象
     */
    public E getMapper()
    {
        return sqlSessionTemplate.getConfiguration().getMapper(mapperClass, getSqlSession());
    }

    @SuppressWarnings("unchecked")
    public BaseMapper<T> getBaseMapper()
    {
        return (BaseMapper<T>)this.getMapper();
    }
    @Override
    public int deleteByPrimaryKey(Integer primaryKey) {
        return this.getBaseMapper().deleteByPrimaryKey(primaryKey);
    }

    @Override
    public int insert(T object) {
        return this.getBaseMapper().insert(object);
    }

    @Override
    public int insertSelective(T object) {
        return this.getBaseMapper().insert(object);
    }

    @Override
    public T selectByPrimaryKey(Integer primaryKey) {
        return this.getBaseMapper().selectByPrimaryKey(primaryKey);
    }

    @Override
    public int updateByPrimaryKeySelective(T object) {
        return this.getBaseMapper().updateByPrimaryKeySelective(object);
    }

    @Override
    public int updateByPrimaryKey(T object) {
        return this.getBaseMapper().updateByPrimaryKey(object);
    }

}
